package com.javatest.springboot3minio.advice;

import io.undertow.server.handlers.form.MultiPartParserDefinition;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartException;

/**
 * Created with IntelliJ IDEA. Description:
 *
 * @author lzy
 * @since 2024-01-12 上午10:20
 */
@ControllerAdvice
public class CommonAdvice {
  private static final Logger LOGGER = LoggerFactory.getLogger(CommonAdvice.class);

  @ResponseBody
  @ExceptionHandler(value = MultipartException.class)
  public Object fileUploadExceptionHandler(MultipartException exception) {
    LOGGER.warn("Meet I/O exception: {}", exception.getMessage());
    Map<String, Object> map = new HashMap<>();
    map.put("code", 500);
    Throwable rootCause = exception.getRootCause();
    if (rootCause instanceof MultiPartParserDefinition.FileTooLargeException) {
      map.put("msg", "文件太大");
    }
    return map;
  }
}
